import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import react from '@vitejs/plugin-react'
import { createHash } from 'node:crypto'

function md5(str = '') {
  return createHash('md5').update(str).digest('hex').slice(0, 7)
}

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()]
  },
  preload: {
    plugins: [externalizeDepsPlugin()],
    build: {
      rollupOptions: {
        input: {
          screen: resolve(__dirname, 'src/preload/screen.ts'),
          index: resolve(__dirname, 'src/preload/index.ts')
        }
      }
    }
  },
  renderer: {
    css: {
      modules: {
        localsConvention: 'camelCaseOnly',
        generateScopedName: (name, fileName) => {
          return name + '_' + md5(fileName)
        }
      }
    },
    resolve: {
      alias: {
        '@renderer': resolve(__dirname, 'src/renderer/src'),
        '@common': resolve(__dirname, 'src/renderer/src/common'),
        '@assets': resolve(__dirname, 'src/renderer/src/assets'),
        '@components': resolve(__dirname, 'src/renderer/src/components'),
        '@pages': resolve(__dirname, 'src/renderer/src/pages'),
        '@utils': resolve(__dirname, 'src/renderer/src/utils'),
        '@styles': resolve(__dirname, 'src/renderer/src/styles'),
        '@model': resolve(__dirname, 'src/renderer/src/model'),
        '@controller': resolve(__dirname, 'src/renderer/src/controller'),
        '@service': resolve(__dirname, 'src/renderer/src/service'),
        '@events': resolve(__dirname, 'src/renderer/src/events'),
        '@config': resolve(__dirname, 'src/renderer/src/config'),
        '~@': resolve(__dirname, 'src')
      }
    },
    plugins: [react()],
    server: {
      port: 5176
    }
  }
})
